﻿using Hydrogen.Cmd.Interfaces;
using Hydrogen.Console;
using System;
using System.Collections.Generic;
using System.IO;
using SConsole = System.Console;

namespace Hydrogen.Cmd.Commands
{
    [CommandFlag("sed", "searchdirectorycommand")]
    class SearchDirectoryCommand : CommandBase
    {
        public new static string Discription { get { return "find Directory with key word"; } }
        public override void Invoke(params string[] args)
        {
            string[] dirs = new string[0];
            List<string> dirList = new List<string>();
            if (args.Length < 2)
            {
                //dirs = Directory.GetDirectories(Environment.CurrentDirectory, "*", SearchOption.AllDirectories);
                GetDirectories(Environment.CurrentDirectory, "*", ref dirList);
                dirs = dirList.ToArray();
            }
            else
            {
                //dirs = Directory.GetDirectories(Environment.CurrentDirectory, args[1], SearchOption.AllDirectories);
                GetDirectories(Environment.CurrentDirectory, args[1], ref dirList);
                dirs = dirList.ToArray();
            }

            List<ConsoleTreeNode> treeNodeList = new List<ConsoleTreeNode>();
            treeNodeList.Add(new ConsoleTreeNode()
            {
                level = 0,
                onDraw = () =>
                {
                    if (dirs.Length <= 0) ConsoleLog.Log("搜索结果为空......也许是无权限读取，试试管理员模式？");
                    else ConsoleLog.Log("搜索结束，共 {0} 条结果。", dirs.Length);
                }
            });
            //foreach (var item in dirs)
            for (int i = 0; i < dirs.Length; i++)
            {
                int index = i;
                treeNodeList.Add(new ConsoleTreeNode()
                {
                    level = 1,
                    onDraw = () =>
                    {
                        ConsoleLog.WriteColor("[{0}] {1}/\t", ConsoleColor.Cyan, index, Path.GetFileName(dirs[index]));
                        ConsoleLog.WriteColorLine(dirs[index], ConsoleColor.Green);
                    }
                });
            }
            ConsoleLog.Write(treeNodeList);

            if (dirs.Length <= 0) return;

            string inputStr = "1";
            while (!string.IsNullOrEmpty(inputStr))
            {
                ConsoleLog.WriteColor("输入编号打开目录，直接回车结束：", ConsoleColor.White);
                inputStr = SConsole.ReadLine();
                if (string.IsNullOrEmpty(inputStr)) return;
                int ind = -1;
                if (int.TryParse(inputStr, out ind) && ind >= 0 && ind < dirs.Length)
                {
                    //打开
                    System.Diagnostics.Process.Start("Explorer.exe", dirs[ind]);
                }
            }
        }

        private void GetDirectories(string path, string searchPattern, ref List<string> result)
        {
            var topDirs = Directory.GetDirectories(path, searchPattern, SearchOption.TopDirectoryOnly);

            result.AddRange(topDirs);
            foreach (var item in topDirs)
            {
                try
                {
                    GetDirectories(item, searchPattern, ref result);
                }
                catch (Exception e)
                {
                    ConsoleLog.LogError(e.Message);
                }
            }
        }
    }
}
